
import time
from Comm import CComm
#import numpy as np
from PyQt5.QtCore import *

class CData():
    def __init__(self, reservedSize = 1):

        self.pointer = 0
        self.data = [0] * reservedSize
        self.name = 'data1'
        self.addr = 0
        self.size = 16
        self.scale = 1.0
        self.offset = 0.0
        self.sign = 'U'
        self.slaveAddr = 1

    def append(self, datum):
        self.data[self.pointer] = datum
        self.pointer = self.pointer + 1
        if self.pointer >= len(self.data):
            self.pointer = 0

    def length(self):
        return self.pointer

    def clear(self):
        self.pointer = 0



class CSampleThread(QThread):

    def __init__(self, dataManager, parent = None):
        super(CSampleThread, self).__init__(parent)

        self.dataMan = dataManager
        self.hold = False

    def run(self):

        #self.timer.start(5)
        self.hold = False

        while self.hold == False:
            self.dataMan.SampleData()
            time.sleep(0.02)

    def stop(self):
        #self.timer.stop()
        self.hold = True


class CDataManager():

    def __init__(self, device):

        self.dataList = [CData(8000*3600*2),
                        CData(8000*3600*2),
                        CData(8000*3600*2),
                        CData(8000*3600*2)]

        self.chCount = 0
        self.fastMode = True

        index = 1
        for data in self.dataList:
            data.name = 'Ch' + str(index)

        self.comm = CComm(device)

        # for debug
        self.count = 0

    def ClearData(self):
        for data in self.dataList:
            data.clear()

    def SampleData(self):
        
        if self.fastMode == True:
            self.comm.GetData(self.chCount, self.dataList)
        else:
            raise 'Not Implemented yet'
            
    def SetFastMode(self, Mode):
        self.fastMode = Mode
#        t = self.count / 10.0
#        v1 = np.sin(t)
#        v2 = np.cos(t)
#        v = np.sin(t*3) * 0.3333333
#        v3 = v1 + v
#        v4 = v2 + v
#        self.data[0].append(v1)
#        self.data[1].append(v2)
#        self.data[2].append(v3)
#        self.data[3].append(v4)
#
#        self.count = self.count + 1

    def GetData(self, ch):
        return self.dataList[ch]

    def StartSample(self):

        self.chCount = 0
        data = bytearray()
        for ch in range(0, len(self.dataList)):
            if self.dataList[ch].addr != 0:
                data.append(self.dataList[ch].addr & 0xFF)
                data.append((self.dataList[ch].addr >> 8) & 0xFF)
                data.append((self.dataList[ch].addr >> 16) & 0xFF)
                data.append((self.dataList[ch].addr >> 24) & 0xFF)

                self.chCount = self.chCount + 1

        self.comm.SendRequest(data)

        #todo:

    def UpdateChProperties(self, chIndex, slaveAddr,  name, addr, size, sign, scale, offset):
        
        self.dataList[chIndex].slaveAddr = slaveAddr
        self.dataList[chIndex].name = name
        self.dataList[chIndex].addr = addr
        self.dataList[chIndex].size = size
        self.dataList[chIndex].sign = sign
        self.dataList[chIndex].scale = scale
        self.dataList[chIndex].offset = offset




